home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GXCLIST.C < prev    next >
C/C++ Source or Header  |  1992-03-14  |  38KB  |  1,210 lines

  1. /* Copyright (C) 1991, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gxclist.c */
  21. /* Command list 'device' for Ghostscript. */
  22. #include "memory_.h"
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gxdevice.h"
  26. #include "gxdevmem.h"            /* must precede gxclist.h */
  27. #include "gxclist.h"
  28.  
  29. /* Patch a couple of things possibly missing from stdio.h. */
  30. #ifndef SEEK_SET
  31. #  define SEEK_SET 0
  32. #endif
  33. #ifndef SEEK_END
  34. #  define SEEK_END 2
  35. #endif
  36.  
  37. #define cdev ((gx_device_clist *)dev)
  38.  
  39. /* Forward declarations of procedures */
  40. private dev_proc_open_device(clist_open);
  41. private dev_proc_get_initial_matrix(clist_get_initial_matrix);
  42. private dev_proc_output_page(clist_output_page);
  43. private dev_proc_map_rgb_color(clist_map_rgb_color);
  44. private dev_proc_map_color_rgb(clist_map_color_rgb);
  45. private dev_proc_fill_rectangle(clist_fill_rectangle);
  46. private dev_proc_tile_rectangle(clist_tile_rectangle);
  47. private dev_proc_copy_mono(clist_copy_mono);
  48. private dev_proc_copy_color(clist_copy_color);
  49. private dev_proc_get_bits(clist_get_bits);
  50. private dev_proc_get_props(clist_get_props);
  51. private dev_proc_put_props(clist_put_props);
  52.  
  53. /* The device descriptor */
  54. private gx_device_procs clist_procs =
  55. {    clist_open,
  56.     clist_get_initial_matrix,
  57.     gx_default_sync_output,
  58.     clist_output_page,
  59.     gx_default_close_device,
  60.     clist_map_rgb_color,
  61.     clist_map_color_rgb,
  62.     clist_fill_rectangle,
  63.     clist_tile_rectangle,
  64.     clist_copy_mono,
  65.     clist_copy_color,
  66.     gx_default_draw_line,
  67.     clist_get_bits,
  68.     clist_get_props,
  69.     clist_put_props
  70. };
  71. gx_device_clist gs_clist_device =
  72. {    sizeof(gx_device_clist),
  73.     &clist_procs,
  74.     "command list",
  75.     0, 0, 1, 1, no_margins, dci_black_and_white, 0,    /* generic */
  76.     NULL, NULL, 0
  77. };
  78.  
  79. /* ------ Define the command set and syntax ------ */
  80.  
  81. /* A command always consists of an operation followed by operands. */
  82. /* The operands are implicit in the procedural code. */
  83. typedef enum {
  84.     cmd_op_misc = 0x00,        /* (see below) */
  85.       cmd_opv_end_run = 0x00,    /* (nothing) */
  86.       cmd_opv_set_tile_size = 0x01,    /* width, height */
  87.       cmd_opv_set_tile_phase = 0x02, /* x, y */
  88.     cmd_op_set_color0 = 0x10,    /* color+2 in op byte, [color] */
  89.     cmd_op_set_color1 = 0x20,    /* color+2 in op byte, [color] */
  90.     cmd_op_set_tile_index = 0x30,    /* index */
  91.     cmd_op_fill_rect = 0x40,    /* rect */
  92.     cmd_op_fill_rect_short = 0x50,    /* dh in op byte,dx,dw | rect_short */
  93.     cmd_op_fill_rect_tiny = 0x60,    /* dw in op byte, rect_tiny */
  94.     cmd_op_tile_rect = 0x70,    /* rect */
  95.     cmd_op_tile_rect_short = 0x80,    /* dh in op byte,dx,dw | rect_short */
  96.     cmd_op_tile_rect_tiny = 0x90,    /* dw in op byte, rect_tiny */
  97.     cmd_op_copy_mono = 0xa0,    /* rect, data_x, raster | */
  98.                     /* d_x+1 in op byte, x, y, w, h */
  99.     cmd_op_copy_color = 0xb0,    /* rect, data_x, raster */
  100.     cmd_op_set_tile_bits = 0xc0,    /* index, <bits> */
  101.     cmd_op_delta_tile_bits = 0xd0,    /* n-1 in op byte, n x <offset, bits> */
  102.     cmd_op_end
  103. } gx_cmd_op;
  104. /* Define the size of the largest command, */
  105. /* not counting any bitmap. */
  106. #define w sizeof(short)            /* size of coordinate */
  107. #define d 3                /* size of tile delta */
  108. private uint cmd_largest_size =
  109.   max(1 + 6 * sizeof(short) /* copy_mono */, 2 + 16 * d /* delta_tile 15 */);
  110. #undef d
  111. #undef w
  112. #ifdef DEBUG
  113. private char *cmd_op_names[16] = {
  114.   "misc", "set_color_0", "set_color_1", "set_tile",
  115.   "fill_rect", "fill_rect_short", "fill_rect_tiny", "tile_rect",
  116.   "tile_rect_short", "tile_rect_tiny", "copy_mono", "copy_color",
  117.   "set_tile_bits", "delta_tile_bits", "?e0?", "?f0?"
  118. };
  119. private char *cmd_misc_op_names[16] = {
  120.   "end_run", "set_tile_size", "set_tile_phase", "?03?",
  121.   "?04?", "?05?", "?06?", "?07?",
  122.   "?08?", "?09?", "?0a?", "?0b?",
  123.   "?0c?", "?0d?", "?0e?", "?0f?"
  124. };
  125. private ulong cmd_op_counts[256];
  126. private ulong cmd_tile_count, cmd_copy_count, cmd_delta_tile_count;
  127. private ulong cmd_tile_reset, cmd_tile_found, cmd_tile_added;
  128. private int
  129. count_op(int op)
  130. {    ++cmd_op_counts[op];
  131. if ( gs_debug['L'] )
  132.     dprintf2(", %s %d\n", cmd_op_names[op >> 4], op & 0xf),
  133.     fflush(dstderr);
  134.     return op;
  135. }
  136. #  define count_add(v, n) (v += (n))
  137. #else
  138. #  define count_op(store_op) store_op
  139. #  define count_add(v, n) 0
  140. #endif
  141. #define count_add1(v) count_add(v, 1)
  142.  
  143. typedef struct {
  144.     short x, y, width, height;
  145. } gx_cmd_rect;
  146. typedef struct {
  147.     byte dx, dwidth, dy, dheight;    /* dy and dheight are optional */
  148. } gx_cmd_rect_short;
  149. #define cmd_min_short (-128)
  150. #define cmd_max_short 127
  151. typedef struct {
  152.     unsigned dx : 4;
  153.     unsigned dy : 4;
  154. } gx_cmd_rect_tiny;
  155. #define cmd_min_tiny (-8)
  156. #define cmd_max_tiny 7
  157.  
  158. /* Define the prefix on each command run in the writing buffer. */
  159. typedef struct cmd_prefix_s cmd_prefix;
  160. struct cmd_prefix_s {
  161.     cmd_prefix *next;
  162.     uint size;
  163. };
  164.  
  165. /* Define the entries in the block file. */
  166. typedef struct cmd_block_s {
  167.     int band;
  168.     long pos;            /* starting position in cfile */
  169. } cmd_block;
  170.  
  171. /* Remember the current state of one band when writing or reading. */
  172. struct gx_clist_state_s {
  173.     gx_color_index color0, color1;    /* most recent colors */
  174.     tile_slot *tile;        /* most recent tile */
  175.     gs_int_point tile_phase;    /* most recent tile phase */
  176.     gx_cmd_rect rect;        /* most recent rectangle */
  177.     /* Following are only used when writing */
  178.     cmd_prefix *head, *tail;    /* list of commands for band */
  179. };
  180. private tile_slot no_tile = { ~0L };
  181.  
  182. /* The initial values for a band state */
  183. private gx_clist_state cls_initial =
  184.    {    gx_no_color_index, gx_no_color_index, &no_tile,
  185.      { 0, 0 }, { 0, 0, 0, 0 },
  186.     0, 0
  187.    };
  188.  
  189. /* Define the size of the command buffer used for reading. */
  190. /* This is needed to split up very large copy_ operations. */
  191. #define cbuf_size 500
  192.  
  193. /* Initialize the device state */
  194. private void clist_init_tiles(P1(gx_device_clist *));
  195. private int
  196. clist_open(gx_device *dev)
  197. {    /*
  198.      * The buffer area (data, data_size) holds a tile cache and a
  199.      * set of block range bit masks when both writing and reading.
  200.      * The rest of the space is used for
  201.      * the command buffer and band state bookkeeping when writing,
  202.      * and for the rendering buffer (image device) when reading.
  203.      * For the moment, we divide the space up arbitrarily.
  204.      */
  205.     byte *data = cdev->data;
  206.     uint size = cdev->data_size;
  207. #define alloc_data(n) data += (n), size -= (n)
  208.     gx_device *target = cdev->target;
  209.     int raster, nbands, band;
  210.     gx_clist_state *states;
  211.     uint state_size;
  212.     cdev->ymin = cdev->ymax = -1;    /* render_init not done yet */
  213.     cdev->tile_data = data;
  214.     cdev->tile_data_size = (size / 5) & -4;    /* arbitrary! */
  215.     alloc_data(cdev->tile_data_size);
  216.     raster = (((target->width * target->color_info.depth + 31) >> 5) << 2) + sizeof(byte *);
  217.     cdev->band_height = size / (uint)raster;
  218.     nbands = target->height / cdev->band_height + 1;
  219.     cdev->nbands = nbands;
  220. #ifdef DEBUG
  221. if ( gs_debug['l'] | gs_debug['L'] )
  222.     dprintf4("[l]width=%d, raster=%d, band_height=%d, nbands=%d\n",
  223.              target->width, raster, cdev->band_height, cdev->nbands);
  224. #endif
  225.     state_size = nbands * sizeof(gx_clist_state);
  226.     if ( state_size > size / 2 )
  227.       return -1;        /* not enough room */
  228.     cdev->mdev.base = data;
  229.     cdev->states = states = (gx_clist_state *)data;
  230.     alloc_data(state_size);
  231.     cdev->cbuf = data;
  232.     cdev->cnext = data;
  233.     cdev->cend = data + size;
  234.     cdev->ccls = 0;
  235.     for ( band = 0; band < nbands; band++, states++ )
  236.       *states = cls_initial;
  237. #undef alloc_data
  238.     cdev->tile_band_mask_size = (nbands + 31) / 32 * 4;
  239.     cdev->tile_max_size = cdev->tile_data_size -
  240.         (sizeof(tile_hash) * 2 + sizeof(tile_slot) +
  241.          cdev->tile_band_mask_size);
  242.     clist_init_tiles(cdev);
  243.     return 0;
  244. }
  245.  
  246. /* (Re)initialize the tile cache. */
  247. private void
  248. clist_init_tiles(register gx_device_clist *cldev)
  249. {    gx_clist_state *pcls;
  250.     int i, hc;
  251.     cldev->tile_slot_size =
  252.       sizeof(tile_slot) + cldev->tile_band_mask_size +
  253.       cldev->tile.raster * cldev->tile.size.y;
  254.     cldev->num_tiles = cldev->tile_data_size /
  255.       (sizeof(tile_hash) * 3 /*(worst case)*/ + cldev->tile_slot_size);
  256.     hc = (cldev->num_tiles - 1) * 2;
  257.     while ( (hc + 1) & hc ) hc |= hc >> 1;    /* make mask */
  258.     if ( hc >= cldev->num_tiles * 3 ) hc >>= 1;
  259.     if ( hc > 255 )        /* slot index in set_tile is only 1 byte */
  260.        {    hc = 255;
  261.         if ( cldev->num_tiles > 200 ) cldev->num_tiles = 200;
  262.        }
  263.     cldev->tile_hash_mask = hc;
  264.     hc++;                /* make actual size */
  265. #ifdef DEBUG
  266. if ( gs_debug['l'] | gs_debug['L'] )
  267.     dprintf5("[l]tile.size=%dx%d, slot_size=%d, num_tiles=%d, hc=%d\n",
  268.          cldev->tile.size.x, cldev->tile.size.y,
  269.          cldev->tile_slot_size, cldev->num_tiles, hc);
  270. #endif
  271.     cldev->tile_hash_table =
  272.         (tile_hash *)(cldev->tile_data + cldev->tile_data_size) - hc;
  273.     cldev->tile_count = 0;
  274.     memset(cldev->tile_data, 0, cldev->tile_data_size);
  275.     memset(cldev->tile_hash_table, -1, hc * sizeof(tile_hash));
  276.     for ( i = 0, pcls = cldev->states; i < cldev->nbands; i++, pcls++ )
  277.         pcls->tile = &no_tile;
  278.     count_add1(cmd_tile_reset);
  279. }
  280.  
  281. /* Forward the non-displaying operations to the target device. */
  282. private void
  283. clist_get_initial_matrix(gx_device *dev, gs_matrix *pmat)
  284. {    (*cdev->target->procs->get_initial_matrix)(dev, pmat);
  285. }
  286. private gx_color_index
  287. clist_map_rgb_color(gx_device *dev, gx_color_value red, gx_color_value green,
  288.   gx_color_value blue)
  289. {    return (*cdev->target->procs->map_rgb_color)(dev, red, green, blue);
  290. }
  291. private int
  292. clist_map_color_rgb(gx_device *dev, gx_color_index color,
  293.   gx_color_value rgb[3])
  294. {    return (*cdev->target->procs->map_color_rgb)(dev, color, rgb);
  295. }
  296. private int
  297. clist_get_props(gx_device *dev, gs_prop_item *plist)
  298. {    gx_device *tdev = cdev->target;
  299.     return (*tdev->procs->get_props)(tdev, plist);
  300. }
  301. private int
  302. clist_put_props(gx_device *dev, gs_prop_item *plist, int count)
  303. {    gx_device *tdev = cdev->target;
  304.     return (*tdev->procs->put_props)(tdev, plist, count);
  305. }
  306.  
  307. /* Print a bitmap for tracing */
  308. #ifdef DEBUG
  309. private void
  310. cmd_print_bits(byte *data, int height, int raster)
  311. {    int i, j;
  312.     for ( i = 0; i < height; i++ )
  313.        {    byte *row = data + i * raster;
  314.         dprintf("[L]");
  315.         for ( j = 0; j < raster; j++ )
  316.           dprintf1(" %02x", row[j]);
  317.         dputc('\n');
  318.        }
  319. }
  320. #else
  321. #  define cmd_print_bits(data, height, raster)
  322. #endif
  323.  
  324. /* ------ Writing ------ */
  325.  
  326. /* Utilities */
  327.  
  328. #define cmd_set_rect(rect)\
  329.   ((rect).x = x, (rect).y = y,\
  330.    (rect).width = width, (rect).height = height)
  331.  
  332. #define clist_write(f, str, len)\
  333.   fwrite(str, 1, len, f)
  334.  
  335. /* Write out the buffered commands, and reset the buffer. */
  336. private void
  337. cmd_write_buffer(gx_device_clist *cldev)
  338. {    FILE *cfile = cldev->cfile;
  339.     FILE *bfile = cldev->bfile;
  340.     int nbands = cldev->nbands;
  341.     gx_clist_state *pcls;
  342.     int band;
  343.     for ( band = 0, pcls = cldev->states; band < nbands; band++, pcls++ )
  344.        {    cmd_prefix *cp = pcls->head;
  345.         if ( cp != 0 )
  346.            {    cmd_block cb;
  347.             cb.band = band;
  348.             cb.pos = ftell(cfile);
  349.             clist_write(bfile, (byte *)&cb, sizeof(cb));
  350.             for ( ; cp != 0; cp = cp->next )
  351.               clist_write(cfile, (byte *)(cp + 1), cp->size);
  352.             pcls->head = pcls->tail = 0;
  353.             fputc(cmd_opv_end_run, cfile);
  354.            }
  355.        }
  356.     cldev->cnext = cldev->cbuf;
  357.     cldev->ccls = 0;
  358. }
  359.  
  360. /* Add a command to the appropriate band list, */
  361. /* and allocate space for its data. */
  362. /* Return the pointer to the data area. */
  363. private byte *
  364. cmd_put_op(gx_device_clist *cldev, gx_clist_state *pcls, uint size)
  365. {    byte *dp = cldev->cnext;
  366. #ifdef DEBUG
  367. if ( gs_debug['L'] )
  368.     dprintf3("[L]band %d: size=%u, left=%d",
  369.              (int)(pcls - cldev->states), size, (int)(cldev->cend - dp));
  370. #endif
  371.     if ( size + (sizeof(cmd_prefix) + 4) > cldev->cend - dp )
  372.       { cmd_write_buffer(cldev);
  373.         return cmd_put_op(cldev, pcls, size);
  374.       }
  375.     if ( cldev->ccls == pcls )
  376.       { /* We're adding another command for the same band. */
  377.         /* Tack it onto the end of the previous one. */
  378.         pcls->tail->size += size;
  379.       }
  380.     else
  381.       { cmd_prefix *cp = (cmd_prefix *)(dp + (((byte *)0 - dp) & 3));
  382.         dp = (byte *)(cp + 1);
  383.         if ( pcls->tail != 0 ) pcls->tail->next = cp;
  384.         else pcls->head = cp;
  385.         pcls->tail = cp;
  386.         cldev->ccls = pcls;
  387.         cp->next = 0;
  388.         cp->size = size;
  389.       }
  390.     cldev->cnext = dp + size;
  391.     return dp;
  392. }
  393.  
  394. /* We store all short quantities little-endian. */
  395. /* This is OK, because we read them back little-endian explicitly. */
  396. #define cmd_putw(w, dp)\
  397.   (*dp = (w) & 0xff, dp[1] = (w) >> 8, dp += 2)
  398.  
  399. /* Write a short bitmap.  1 <= bwidth <= 3. */
  400. private void
  401. cmd_put_short_bits(register byte *dp, register byte *data,
  402.   int raster, register int bwidth, register int height)
  403. {    while ( --height >= 0 )
  404.        {    switch ( bwidth )
  405.            {
  406.         case 3: dp[2] = data[2];
  407.         case 2: dp[1] = data[1];
  408.         case 1: dp[0] = data[0];
  409.            }
  410.         dp += bwidth, data += raster;
  411.        }
  412. }
  413.  
  414. private int
  415. cmd_write_rect_cmd(gx_device *dev, gx_clist_state *pcls,
  416.   int op, int x, int y, int width, int height)
  417. {    int dx = x - pcls->rect.x;
  418.     int dy = y - pcls->rect.y;
  419.     int dwidth = width - pcls->rect.width;
  420.     int dheight = height - pcls->rect.height;
  421. #define check_ranges_1()\
  422.   ((unsigned)(dx - rmin) <= (rmax - rmin) &&\
  423.    (unsigned)(dy - rmin) <= (rmax - rmin) &&\
  424.    (unsigned)(dwidth - rmin) <= (rmax - rmin))
  425. #define check_ranges()\
  426.   (check_ranges_1() &&\
  427.    (unsigned)(dheight - rmin) <= (rmax - rmin))
  428. #define rmin cmd_min_tiny
  429. #define rmax cmd_max_tiny
  430.     cmd_set_rect(pcls->rect);
  431.     if ( dheight == 0 && check_ranges_1() )
  432.        {    byte *dp = cmd_put_op(cdev, pcls, 2);
  433.         count_op(*dp = op + 0x20 + dwidth - rmin);
  434.         dp[1] = (dx << 4) + dy - (rmin * 0x11);
  435.        }
  436. #undef rmin
  437. #undef rmax
  438. #define rmin cmd_min_short
  439. #define rmax cmd_max_short
  440.     else if ( check_ranges() )
  441.        {    int dh = dheight - cmd_min_tiny;
  442.         byte *dp;
  443.         if ( (unsigned)dh <= cmd_max_tiny - cmd_min_tiny && dh != 0 &&
  444.              dy == 0
  445.            )
  446.            {    op += dh;
  447.             dp = cmd_put_op(cdev, pcls, 3);
  448.            }
  449.         else
  450.            {    dp = cmd_put_op(cdev, pcls, 5);
  451.             dp[3] = dy - rmin;
  452.             dp[4] = dheight - rmin;
  453.            }
  454.         count_op(*dp = op + 0x10);
  455.         dp[1] = dx - rmin;
  456.         dp[2] = dwidth - rmin;
  457.        }
  458.     else
  459.        {    byte *dp = cmd_put_op(cdev, pcls, 1 + sizeof(pcls->rect));
  460.         count_op(*dp = op);
  461.         memcpy(dp + 1, &pcls->rect, sizeof(pcls->rect));
  462.        }
  463.     return 0;
  464. }
  465.  
  466. private void
  467. cmd_put_color(gx_device *dev, gx_clist_state *pcls,
  468.   int op, gx_color_index color)
  469. {    if ( (long)color >= -1 && (long)color <= 13 )
  470.         count_op(*cmd_put_op(cdev, pcls, 1) = op + (int)color + 2);
  471.     else
  472.        {    byte *dp = cmd_put_op(cdev, pcls, 1 + sizeof(color));
  473.         count_op(*dp = op);
  474.         memcpy(dp + 1, &color, sizeof(color));
  475.        }
  476. }
  477. private void
  478. cmd_set_colors(gx_device *dev, gx_clist_state *pcls,
  479.   gx_color_index color0, gx_color_index color1)
  480. {    if ( color0 != pcls->color0 )
  481.        {    cmd_put_color(dev, pcls, cmd_op_set_color0, color0);
  482.         pcls->color0 = color0;
  483.        }
  484.     if ( color1 != pcls->color1 )
  485.        {    cmd_put_color(dev, pcls, cmd_op_set_color1, color1);
  486.         pcls->color1 = color1;
  487.        }
  488. }
  489.  
  490. /* Driver interface */
  491.  
  492. /* Macros for dividing up a single call into bands */
  493. #define BEGIN_RECT\
  494.    {    int yend = y + height;\
  495.     int band_height = cdev->band_height;\
  496.     do\
  497.        {    int band = y / band_height;\
  498.         gx_clist_state *pcls = cdev->states + band;\
  499.         height = band_height - y % band_height;\
  500.         if ( yend - y < height ) height = yend - y;\
  501.            {
  502. #define END_RECT\
  503.            }\
  504.         y += height;\
  505.        }\
  506.     while ( y < yend );\
  507.    }
  508.  
  509. private int
  510. clist_fill_rectangle(gx_device *dev, int x, int y, int width, int height,
  511.   gx_color_index color)
  512. {    BEGIN_RECT
  513.     if ( color != pcls->color1 )
  514.         cmd_set_colors(dev, pcls, pcls->color0, color);
  515.     cmd_write_rect_cmd(dev, pcls, cmd_op_fill_rect, x, y, width, height);
  516.     END_RECT
  517.     return 0;
  518. }
  519.  
  520. /* Compare unequal tiles.  Return -1 if unrelated, */
  521. /* or 2<=N<=50 for the size of the delta encoding. */
  522. private int
  523. tile_diff(byte *old_data, byte *new_data, uint tsize, byte _ss *delta)
  524. {    register ushort *old2, *new2;
  525.     register ushort diff;
  526.     int count;
  527.     register int i;
  528.     byte _ss *pd;
  529.     if ( tsize > 128 ) return -1;
  530.     old2 = (ushort *)old_data;
  531.     new2 = (ushort *)new_data;
  532.     count = 0;
  533.     pd = delta + 2;            /* skip slot index */
  534.     for ( i = 0; i < tsize; i += 2, old2++, new2++ )
  535.       if ( (diff = *new2 ^ *old2) != 0 )
  536. #if arch_is_big_endian
  537. #  define i_hi 0
  538. #  define b_0(w) ((w) >> 8)
  539. #  define b_1(w) ((byte)(w))
  540. #else
  541. #  define i_hi 1
  542. #  define b_0(w) ((byte)(w))
  543. #  define b_1(w) ((w) >> 8)
  544. #endif
  545.        {    if ( count == 16 ) return -1;
  546.         if ( diff & 0xff00 )
  547.            {    if ( diff & 0xff )
  548.                 *pd++ = 0x80 + i,
  549.                 *pd++ = b_0(diff),
  550.                 *pd++ = b_1(diff);
  551.             else
  552.                 *pd++ = i + i_hi, *pd++ = diff >> 8;
  553.            }
  554.         else            /* know diff != 0 */
  555.             *pd++ = i + (1 - i_hi), *pd++ = (byte)diff;
  556.         count++;
  557.        }
  558. #undef b_0
  559. #undef b_1
  560. #undef i_hi
  561.     delta[0] = (byte)cmd_op_delta_tile_bits + count - 1;
  562.     return pd - delta;
  563. }
  564.  
  565. /* Handle changing tiles for clist_tile_rectangle. */
  566. /* We put this in a separate routine, even though it is called only once, */
  567. /* to avoid cluttering up the main-line case of tile_rectangle. */
  568. private int
  569. clist_change_tile(gx_device_clist *cldev, gx_clist_state *pcls,
  570.   gx_bitmap *tile)
  571. {    uint tile_size = tile->raster * tile->size.y;
  572.     tile_slot *old_tile, *new_tile;
  573.     int slot_index;
  574.     if ( pcls->tile == &no_tile )
  575.        {    byte *dp;
  576.         if ( tile->raster != ((tile->size.x + 31) >> 5) << 2 ||
  577.              tile_size > cldev->tile_max_size
  578.            )
  579.             return -1;
  580.         if ( tile->size.x != cldev->tile.size.x ||
  581.              tile->size.y != cldev->tile.size.y
  582.            )
  583.            {    cldev->tile = *tile;    /* reset size, raster */
  584.             clist_init_tiles(cldev);
  585.            }
  586.         dp = cmd_put_op(cldev, pcls, 1 + sizeof(cldev->tile.size));
  587.         count_op(*dp = (byte)cmd_opv_set_tile_size);
  588.         memcpy(dp + 1, &cldev->tile.size, sizeof(cldev->tile.size));
  589.        }
  590.     /* Look up the tile in the cache. */
  591. top:       {    gx_bitmap_id id = tile->id;
  592.         uint probe = (uint)(id >> 16) + (uint)(id);
  593.         old_tile = pcls->tile;
  594.         for ( ; ; probe += 25 /* semi-random odd # */ )
  595.            {    tile_hash *hptr = cldev->tile_hash_table +
  596.               (probe & cldev->tile_hash_mask);
  597.             if ( (slot_index = hptr->slot_index) < 0 ) /* empty entry */
  598.                {    if ( cldev->tile_count == cldev->num_tiles )
  599.                    {    /* Punt. */
  600.                     clist_init_tiles(cldev);
  601.                     goto top;
  602.                    }
  603.                 hptr->slot_index = slot_index =
  604.                   cldev->tile_count++;
  605.                 new_tile = tile_slot_ptr(cldev, slot_index);
  606.                 new_tile->id = id;
  607.                 memcpy(ts_bits(cldev, new_tile), tile->data, tile_size);
  608.                 count_add1(cmd_tile_added);
  609. #ifdef DEBUG
  610. if ( gs_debug['L'] )
  611.                 dprintf3("[L]adding tile %d, hash=%d, id=%lx\n",
  612.                      slot_index, hptr - cldev->tile_hash_table,
  613.                      id);
  614. #endif
  615.                 break;
  616.                }
  617.             new_tile = tile_slot_ptr(cldev, slot_index);
  618.             if ( new_tile->id == id )
  619.                {    count_add1(cmd_tile_found);
  620. #ifdef DEBUG
  621. if ( gs_debug['L'] )
  622.                 dprintf1("[L]found tile %d\n", slot_index);
  623. #endif
  624.                 break;
  625.                }
  626.            }
  627.        }
  628.     /* Check whether this band knows about this tile yet. */
  629.        {    int band_index = pcls - cldev->states;
  630.         byte pmask = 1 << (band_index & 7);
  631.         byte *ppresent = ts_mask(new_tile) + (band_index >> 3);
  632.         if ( *ppresent & pmask )
  633.            {    /* Tile is known, just put out the index. */
  634.             byte *dp = cmd_put_op(cldev, pcls, 2);
  635.             count_op(*dp = cmd_op_set_tile_index);
  636.             dp[1] = slot_index;
  637.            }
  638.         else
  639.            {    /* Tile is not known, put out the bits.  Use a */
  640.             /* delta encoding or a short encoding if possible. */
  641.             byte *new_data = ts_bits(cldev, new_tile);
  642.             byte *dp;
  643.             byte delta[2+16*3];
  644.             int diff;
  645.             *ppresent |= pmask;
  646.             if ( old_tile != &no_tile &&
  647.                  (diff = tile_diff(ts_bits(cldev, old_tile), new_data, tile_size, delta)) >= 0
  648.                )
  649.                {    /* Use delta representation */
  650.                 dp = cmd_put_op(cldev, pcls, diff);
  651.                 count_op(delta[0]);
  652.                 delta[1] = slot_index;
  653.                 memcpy(dp, delta, diff);
  654.                 count_add(cmd_delta_tile_count, diff - 2);
  655.                }
  656.             else
  657.                {    if ( tile->size.x <= 16 )
  658.                    {    dp = cmd_put_op(cldev, pcls, 2 + (tile_size >> 1));
  659.                     cmd_put_short_bits(dp + 2, new_data, tile->raster, 2, tile->size.y);
  660.                     count_add(cmd_tile_count, tile_size >> 1);
  661.                    }
  662.                 else
  663.                    {    dp = cmd_put_op(cldev, pcls, 2 + tile_size);
  664.                     memcpy(dp + 2, new_data, tile_size);
  665.                     count_add(cmd_tile_count, tile_size);
  666.                    }
  667.                 count_op(*dp = (byte)cmd_op_set_tile_bits);
  668.                 dp[1] = slot_index;
  669.                }
  670.            }
  671.        }
  672.     pcls->tile = new_tile;
  673.     return 0;
  674. }
  675. private int
  676. clist_tile_rectangle(gx_device *dev, gx_bitmap *tile, int x, int y,
  677.   int width, int height, gx_color_index color0, gx_color_index color1,
  678.   int px, int py)
  679. {    BEGIN_RECT
  680.     if ( tile->id != pcls->tile->id )
  681.        {    if ( clist_change_tile(cdev, pcls, tile) < 0 )
  682.             return gx_default_tile_rectangle(dev, tile, x, y, width, height, color0, color1, px, py);
  683.        }
  684.     if ( color0 != pcls->color0 || color1 != pcls->color1 )
  685.         cmd_set_colors(dev, pcls, color0, color1);
  686.     if ( px != pcls->tile_phase.x || py != pcls->tile_phase.y )
  687.        {    byte *dp = cmd_put_op(cdev, pcls, 1 + sizeof(pcls->tile_phase));
  688.         count_op(*dp = (byte)cmd_opv_set_tile_phase);
  689.         pcls->tile_phase.x = px;
  690.         pcls->tile_phase.y = py;
  691.         memcpy(dp + 1, &pcls->tile_phase, sizeof(pcls->tile_phase));
  692.        }
  693.     cmd_write_rect_cmd(dev, pcls, cmd_op_tile_rect, x, y, width, height);
  694.     END_RECT
  695.     return 0;
  696. }
  697.  
  698. private int
  699. clist_copy_mono(gx_device *dev,
  700.     byte *data, int data_x, int raster, gx_bitmap_id id,
  701.     int x, int y, int width, int height,
  702.     gx_color_index color0, gx_color_index color1)
  703. {    int y0 = y;
  704.     BEGIN_RECT
  705.     gx_cmd_rect rect;
  706.     uint dsize;
  707.     int bwidth;
  708.     byte *row = data + (y - y0) * raster;
  709.     byte *dp;
  710.     if ( color0 != pcls->color0 || color1 != pcls->color1 )
  711.         cmd_set_colors(dev, pcls, color0, color1);
  712.     cmd_set_rect(rect);
  713.     if ( width >= 2 && (bwidth = (width + (data_x & 7) + 7) >> 3) <= 3 &&
  714.         height <= min(255, (cbuf_size - (1 + 2 * 2 + 2)) / bwidth)
  715.        )
  716.        {    dsize = height * bwidth;
  717.         dp = cmd_put_op(cdev, pcls, 1 + 2 * 2 + 2 + dsize);
  718.         count_op(*dp++ = (byte)cmd_op_copy_mono + (data_x & 7) + 1);
  719.         cmd_putw(x, dp);
  720.         cmd_putw(y, dp);
  721.         *dp++ = width;
  722.         *dp++ = height;
  723.         row += data_x >> 3;
  724.         cmd_put_short_bits(dp, row, raster, bwidth, height);
  725.        }
  726.     else
  727.        {    dsize = height * raster;
  728.         if ( dsize > cbuf_size )
  729.            {    /* We have to split it into pieces. */
  730.             if ( height > 1 )
  731.                {    int h2 = height >> 1;
  732.                 clist_copy_mono(dev, data, data_x, raster,
  733.                     gx_no_bitmap_id, x, y, width, h2,
  734.                     color0, color1);
  735.                 return clist_copy_mono(dev, data + h2 * raster,
  736.                     data_x, raster, gx_no_bitmap_id,
  737.                     x, y + h2, width, height - h2,
  738.                     color0, color1);
  739.                }
  740.             /* Split a single (very long) row. */
  741.                {    int w2 = width >> 1;
  742.                 clist_copy_mono(dev, data, data_x, raster,
  743.                     gx_no_bitmap_id, x, y, w2, 1,
  744.                     color0, color1);
  745.                 return clist_copy_mono(dev, data, data_x + w2,
  746.                     raster, gx_no_bitmap_id, x + w2, y,
  747.                     width - w2, 1, color0, color1);
  748.                }
  749.            }
  750.         dp = cmd_put_op(cdev, pcls, 1 + sizeof(rect) + 4 + dsize);
  751.         count_op(*dp++ = (byte)cmd_op_copy_mono);
  752.         memcpy(dp, (byte *)&rect, sizeof(rect));
  753.         dp += sizeof(rect);
  754.         cmd_putw(data_x, dp);
  755.         cmd_putw(raster, dp);
  756.         memcpy(dp, row, dsize);
  757.        }
  758.     pcls->rect = rect;
  759.     count_add(cmd_copy_count, dsize);
  760.     END_RECT
  761.     return 0;
  762. }
  763.  
  764. private int
  765. clist_copy_color(gx_device *dev,
  766.     byte *data, int data_x, int raster, gx_bitmap_id id,
  767.     int x, int y, int width, int height)
  768. {    int y0 = y;
  769.     BEGIN_RECT
  770.     gx_cmd_rect rect;
  771.     uint dsize = height * raster;
  772.     byte *dp;
  773.     if ( dsize > cbuf_size )
  774.        {    /* We have to split it into pieces. */
  775.         if ( height > 1 )
  776.            {    int h2 = height >> 1;
  777.             clist_copy_color(dev, data, data_x, raster,
  778.                 gx_no_bitmap_id, x, y, width, h2);
  779.             return clist_copy_color(dev, data + h2 * raster, gx_no_bitmap_id,
  780.                 data_x, raster, x, y + h2, width, height - h2);
  781.            }
  782.         /* Split a single (very long) row. */
  783.            {    int w2 = width >> 1;
  784.             clist_copy_color(dev, data, data_x, raster,
  785.                 gx_no_bitmap_id, x, y, w2, 1);
  786.             return clist_copy_color(dev, data, data_x + w2,
  787.                 raster, gx_no_bitmap_id, x + w2, y,
  788.                 width - w2, 1);
  789.            }
  790.  
  791.        }
  792.     cmd_set_rect(rect);
  793.     dp = cmd_put_op(cdev, pcls, 1 + sizeof(rect) + 4 + dsize);
  794.     count_op(*dp++ = (byte)cmd_op_copy_color);
  795.     memcpy(dp, (byte *)&rect, sizeof(rect));
  796.     pcls->rect = rect;
  797.     dp += sizeof(rect);
  798.     cmd_putw(data_x, dp);
  799.     cmd_putw(raster, dp);
  800.     memcpy(dp, data + (y - y0) * raster, dsize);
  801.     END_RECT
  802.     return 0;
  803. }
  804.  
  805. /* ------ Reading/rendering ------ */
  806.  
  807. /* Clean up after rendering a page. */
  808. private int
  809. clist_output_page(gx_device *dev, int num_copies, int flush)
  810. {    if ( flush )
  811.        {    rewind(cdev->cfile);
  812.         rewind(cdev->bfile);
  813.         cdev->bfile_end_pos = 0;
  814.        }
  815.     else
  816.        {    fseek(cdev->cfile, 0L, SEEK_END);
  817.         fseek(cdev->bfile, 0L, SEEK_END);
  818.        }
  819.     return clist_open(dev);        /* reinitialize */
  820. }
  821.  
  822. private int clist_render_init(P1(gx_device_clist *));
  823. private int clist_render(P3(gx_device_clist *, gx_device *, int));
  824.  
  825. /* Copy scan lines to the client.  This is where rendering gets done. */
  826. private int
  827. clist_get_bits(gx_device *dev, int start_y,
  828.   byte *str, uint size, int pad_to_word)
  829. {    int y = start_y;
  830.     byte *dest = str;
  831.     gx_device_memory *mdev = &cdev->mdev;
  832.     uint bytes_per_line;
  833.     uint count, left;
  834.     /* Initialize for rendering if we haven't done so yet. */
  835.     if ( cdev->ymin < 0 )
  836.         clist_render_init(cdev);
  837.     bytes_per_line = gx_device_bytes_per_scan_line((gx_device *)mdev,
  838.                                pad_to_word);
  839.     count = min(size / bytes_per_line,
  840.             cdev->target->height - start_y);
  841.     /* Render bands and copy them incrementally. */
  842.     for ( left = count; left; )
  843.        {    int n;
  844.         if ( !(y >= cdev->ymin && y < cdev->ymax) )
  845.            {    int band = y / mdev->height;
  846.             int code;
  847.             rewind(cdev->bfile);
  848.             (*mdev->procs->open_device)((gx_device *)mdev);    /* reinitialize */
  849.             code = clist_render(cdev, (gx_device *)mdev, band);
  850.             if ( code < 0 ) return code;
  851.             cdev->ymin = band * mdev->height;
  852.             cdev->ymax = cdev->ymin + mdev->height;
  853.            }
  854.         n = min(cdev->ymax - y, left);
  855.         (*mdev->procs->get_bits)((gx_device *)mdev,
  856.                      y - cdev->ymin, dest,
  857.                      bytes_per_line * n, pad_to_word);
  858.         y += n, dest += bytes_per_line * n, left -= n;
  859.        }
  860.     return count;
  861. }
  862.  
  863. #undef cdev
  864.  
  865. /* Initialize for reading. */
  866. private int
  867. clist_render_init(gx_device_clist *cdev)
  868. {    gx_device *target = cdev->target;
  869.     byte *base = cdev->mdev.base;    /* save */
  870.     int depth = target->color_info.depth;
  871.     uint raster = ((target->width * depth + 31) >> 5) << 2;
  872.     gx_device_memory *mdev = gdev_mem_device_for_bits(depth);
  873.     if ( mdev == 0 )
  874.         return_error(gs_error_rangecheck);
  875.     cmd_write_buffer(cdev);        /* flush buffer */
  876.     /* Write the terminating entry in the block file. */
  877.     /* Note that because of copypage, there may be many such entries. */
  878.        {    cmd_block cb;
  879.         cb.band = -1;
  880.         cb.pos = ftell(cdev->cfile);
  881.         clist_write(cdev->bfile, (byte *)&cb, sizeof(cb));
  882.         cdev->bfile_end_pos = ftell(cdev->bfile);
  883.        }
  884.     cdev->mdev = *mdev;
  885.     cdev->mdev.base = base;        /* restore */
  886.     (*target->procs->get_initial_matrix)(target, &cdev->mdev.initial_matrix);
  887.     cdev->mdev.width = target->width;
  888.     cdev->mdev.height = cdev->band_height;
  889.     cdev->mdev.raster = raster;
  890.     cdev->ymin = cdev->ymax = 0;
  891. #ifdef DEBUG
  892. if ( gs_debug['l'] | gs_debug['L'] )
  893.    {    int ci, cj;
  894.     dprintf3("[l]counts: tile = %ld, copy = %ld, delta = %ld\n",
  895.              cmd_tile_count, cmd_copy_count, cmd_delta_tile_count);
  896.     dprintf3("           reset = %ld, found = %ld, added = %ld\n",
  897.              cmd_tile_reset, cmd_tile_found, cmd_tile_added);
  898.     for ( ci = 0; ci < 0x100; ci += 0x10 )
  899.        {    dprintf1("[l]  %s =", cmd_op_names[ci >> 4]);
  900.         for ( cj = ci; cj < ci + 0x10; cj++ )
  901.             dprintf1(" %ld", cmd_op_counts[cj]);
  902.         dputs("\n");
  903.        }
  904.    }
  905. #endif
  906.     return 0;
  907. }
  908.  
  909. /* Render one band to a specified target device. */
  910. #define assign_getw(var, p)\
  911.   (var = *p + ((uint)p[1] << 8), p += 2)
  912. typedef byte _ss *cb_ptr;
  913. private void clist_read(P3(FILE *, byte *, uint));
  914. private cb_ptr clist_read_short_bits(P6(FILE *, byte *, int, int, cb_ptr, cb_ptr));
  915. private int
  916. clist_render(gx_device_clist *cdev, gx_device *tdev, int band)
  917. {    byte cbuf[cbuf_size];
  918.     byte bits[4 * 255];        /* for short copy_mono bits */
  919.     register cb_ptr cbp;
  920.     cb_ptr cb_limit;
  921.     cb_ptr cb_end;
  922.     FILE *file = cdev->cfile;
  923.     FILE *bfile = cdev->bfile;
  924.     int y0 = band * cdev->band_height;
  925.     gx_clist_state state;
  926.     gx_bitmap state_tile;
  927.     uint tile_bits_size;        /* size of bits of each tile */
  928.     gs_int_point tile_phase;
  929.     cmd_block b_this;
  930.     long pos;
  931.     uint left;
  932. #define cmd_read_var(ptr, cbp)\
  933.   memcpy(ptr, cbp, sizeof(*ptr)),\
  934.   cbp += sizeof(*ptr)
  935. #define cmd_read(ptr, vsize, cbp)\
  936.   if ( cb_end - cbp >= vsize )\
  937.     memcpy(ptr, cbp, vsize), cbp += vsize;\
  938.   else\
  939.    { uint cleft = cb_end - cbp;\
  940.      memcpy(ptr, cbp, cleft); vsize -= cleft;\
  941.      clist_read(file, ptr + cleft, vsize);\
  942.      cbp = cb_end;\
  943.    }
  944. #define cmd_read_short_bits(ptr, bw, ht, cbp)\
  945.   cbp = clist_read_short_bits(file, ptr, bw, ht, cbp, cb_end)
  946.     state = cls_initial;
  947.     state_tile.id = 0;
  948.     tile_phase.x = tile_phase.y = 0;
  949. trd:    clist_read(bfile, (byte *)&b_this, sizeof(b_this));
  950. top:    /* Find the next run of commands for this band. */
  951.     if ( b_this.band < 0 && ftell(bfile) == cdev->bfile_end_pos )
  952.         return 0;    /* end of bfile */
  953.     if ( b_this.band != band ) goto trd;
  954.     pos = b_this.pos;
  955.     clist_read(bfile, (byte *)&b_this, sizeof(b_this));
  956.     fseek(file, pos, SEEK_SET);
  957.     left = (uint)(b_this.pos - pos);
  958.     cb_limit = cbuf + (cbuf_size - cmd_largest_size);
  959.     cb_end = cbuf + cbuf_size;
  960.     cbp = cb_end;
  961.     for ( ; ; )
  962.        {    int op;
  963.         uint bytes;
  964.         int data_x, raster;
  965.         int code;
  966.         cb_ptr source;
  967.         gx_color_index _ss *pcolor;
  968.         /* Make sure the buffer contains a full command. */
  969.         if ( cbp > cb_limit )
  970.            {    uint nread;
  971.             memcpy(cbuf, cbp, cb_end - cbp);
  972.             cbp = cbuf + (cb_end - cbp);
  973.             nread = cb_end - cbp;
  974.             if ( nread > left ) nread = left;
  975.             clist_read(file, cbp, nread);
  976.             cb_end = cbp + nread;
  977.             cbp = cbuf;
  978.             left -= nread;
  979.             if ( cb_limit > cb_end ) cb_limit = cb_end;
  980.            }
  981.         op = *cbp++;
  982. #ifdef DEBUG
  983. if ( gs_debug['L'] )
  984.         dprintf2("[L]%s %d:\n", cmd_op_names[op >> 4], op & 0xf);
  985. #endif
  986.         switch ( op >> 4 )
  987.            {
  988.         case cmd_op_misc >> 4:
  989.             switch ( op )
  990.                {
  991.             case cmd_opv_end_run:
  992.                 goto top;
  993.             case cmd_opv_set_tile_size:
  994.                 cmd_read_var(&state_tile.size, cbp);
  995.                 state_tile.raster = ((state_tile.size.x + 31) >> 5) << 2;
  996.                 /* We can't actually know the rep_size, */
  997.                 /* so we play it safe. */
  998.                 state_tile.rep_width = state_tile.size.x;
  999.                 state_tile.rep_height = state_tile.size.y;
  1000.                 cdev->tile_slot_size = tile_bits_size =
  1001.                     state_tile.raster * state_tile.size.y;
  1002.                 break;
  1003.             case cmd_opv_set_tile_phase:
  1004.                 cmd_read_var(&state.tile_phase, cbp);
  1005.                 break;
  1006.             default:
  1007.                 goto bad_op;
  1008.                }
  1009.             tile_phase.x = state.tile_phase.x % state_tile.size.x;
  1010.             tile_phase.y = (state.tile_phase.y + y0) % state_tile.size.y;
  1011.             continue;
  1012.         case cmd_op_set_color0 >> 4:
  1013.             pcolor = &state.color0;
  1014.             goto set_color;
  1015.         case cmd_op_set_color1 >> 4:
  1016.             pcolor = &state.color1;
  1017. set_color:        if ( op & 0xf )
  1018.                 *pcolor = (gx_color_index)(long)((op & 0xf) - 2);
  1019.             else
  1020.                 cmd_read_var(pcolor, cbp);
  1021.             continue;
  1022.         case cmd_op_set_tile_index >> 4:
  1023.             state_tile.data = (byte *)tile_slot_ptr(cdev, *cbp);
  1024.             cbp++;
  1025.             continue;
  1026.         case cmd_op_copy_mono >> 4:
  1027.             if ( op & 0xf )
  1028.                {    assign_getw(state.rect.x, cbp);
  1029.                 assign_getw(state.rect.y, cbp);
  1030.                 state.rect.width = *cbp++;
  1031.                 state.rect.height = *cbp++;
  1032.                 break;
  1033.                }
  1034.             /* falls through */
  1035.         case cmd_op_fill_rect >> 4:
  1036.         case cmd_op_tile_rect >> 4:
  1037.         case cmd_op_copy_color >> 4:
  1038.             cmd_read_var(&state.rect, cbp);
  1039.             break;
  1040.         case cmd_op_fill_rect_short >> 4:
  1041.         case cmd_op_tile_rect_short >> 4:
  1042.             state.rect.x += *cbp + cmd_min_short;
  1043.             state.rect.width += cbp[1] + cmd_min_short;
  1044.             if ( op & 0xf )
  1045.                {    state.rect.height += (op & 0xf) + cmd_min_tiny;
  1046.                 cbp += 2;
  1047.                }
  1048.             else
  1049.                {    state.rect.y += cbp[2] + cmd_min_short;
  1050.                 state.rect.height += cbp[3] + cmd_min_short;
  1051.                 cbp += 4;
  1052.                }
  1053.             break;
  1054.         case cmd_op_fill_rect_tiny >> 4:
  1055.         case cmd_op_tile_rect_tiny >> 4:
  1056.            {    int txy = *cbp++;
  1057.             state.rect.x += (txy >> 4) + cmd_min_tiny;
  1058.             state.rect.y += (txy & 0xf) + cmd_min_tiny;
  1059.             state.rect.width += (op & 0xf) + cmd_min_tiny;
  1060.            }    break;
  1061.         case cmd_op_set_tile_bits >> 4:
  1062.             state_tile.data = (byte *)tile_slot_ptr(cdev, *cbp);
  1063.             cbp++;
  1064.             if ( state_tile.size.x <= 16 )
  1065.                {    cmd_read_short_bits(state_tile.data, 2, state_tile.size.y, cbp);
  1066.                }
  1067.             else
  1068.                {    bytes = tile_bits_size;
  1069.                 cmd_read(state_tile.data, bytes, cbp);
  1070.                }
  1071. #ifdef DEBUG
  1072. if ( gs_debug['L'] )
  1073.             cmd_print_bits(state_tile.data, state_tile.size.y,
  1074.                        state_tile.raster);
  1075. #endif
  1076.             continue;
  1077.         case cmd_op_delta_tile_bits >> 4:
  1078.            {    byte *new_data = (byte *)tile_slot_ptr(cdev, *cbp);
  1079.             cbp++;
  1080.             memcpy(new_data, state_tile.data, tile_bits_size);
  1081.             state_tile.data = new_data;
  1082.             do
  1083.                {    uint offset = *cbp;
  1084.                 if ( offset < 0x80 )
  1085.                     new_data[offset] ^= cbp[1],
  1086.                     cbp += 2;
  1087.                 else
  1088.                     offset -= 0x80,
  1089.                     new_data[offset] ^= cbp[1],
  1090.                     new_data[offset + 1] ^= cbp[2],
  1091.                     cbp += 3;
  1092.                }
  1093.             while ( op-- & 0xf );
  1094.            }    continue;
  1095.         default:
  1096. bad_op:            printf/*lprintf5*/("Bad op %02x band %d file pos %ld buf pos %d/%d\n",
  1097.                  op, band, ftell(file), (int)(cbp - cbuf), (int)(cb_end - cbuf));
  1098.                {    cb_ptr pp;
  1099.                 for ( pp = cbuf; pp < cb_end; pp += 10 )
  1100.                   printf/*lprintf10*/(" %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  1101.                        pp[0], pp[1], pp[2], pp[3], pp[4],
  1102.                        pp[5], pp[6], pp[7], pp[8], pp[9]);
  1103.                }
  1104.                     
  1105.             gs_exit(1);
  1106.             return -1;
  1107.            }
  1108. #ifdef DEBUG
  1109. if ( gs_debug['L'] )
  1110.         dprintf4("[L]  x=%d y=%d w=%d h=%d\n",
  1111.              state.rect.x, state.rect.y, state.rect.width,
  1112.              state.rect.height);
  1113. #endif
  1114.         switch ( op >> 4 )
  1115.            {
  1116.         case cmd_op_fill_rect >> 4:
  1117.         case cmd_op_fill_rect_short >> 4:
  1118.         case cmd_op_fill_rect_tiny >> 4:
  1119.             code = (*tdev->procs->fill_rectangle)
  1120.               (tdev, state.rect.x, state.rect.y - y0,
  1121.                state.rect.width, state.rect.height, state.color1);
  1122.             break;
  1123.         case cmd_op_tile_rect >> 4:
  1124.         case cmd_op_tile_rect_short >> 4:
  1125.         case cmd_op_tile_rect_tiny >> 4:
  1126.             code = (*tdev->procs->tile_rectangle)
  1127.               (tdev, &state_tile,
  1128.                state.rect.x, state.rect.y - y0,
  1129.                state.rect.width, state.rect.height,
  1130.                state.color0, state.color1,
  1131.                tile_phase.x, tile_phase.y);
  1132.             break;
  1133.         case cmd_op_copy_mono >> 4:
  1134.             if ( op & 0xf )
  1135.                {    data_x = (op & 0xf) - 1;
  1136.                 raster = 4;
  1137.                 cmd_read_short_bits(bits, (data_x + state.rect.width + 7) >> 3, state.rect.height, cbp);
  1138.                 source = bits;
  1139.                 goto copy;
  1140.                }
  1141.             /* falls through */
  1142.         case cmd_op_copy_color >> 4:
  1143.             assign_getw(data_x, cbp);
  1144.             assign_getw(raster, cbp);
  1145.             bytes = state.rect.height * raster;
  1146.             /* copy_mono and copy_color have ensured that */
  1147.             /* the bits will fit in a single buffer. */
  1148.             cmd_read(cbuf, bytes, cbp);
  1149.             source = cbuf;
  1150. copy:
  1151. #ifdef DEBUG
  1152. if ( gs_debug['L'] )
  1153.    {            dprintf2("[L]  data_x=%d raster=%d\n",
  1154.                  data_x, raster);
  1155.             cmd_print_bits(source, state.rect.height, raster);
  1156.    }
  1157. #endif
  1158.             code = (op >> 4 == (byte)cmd_op_copy_mono >> 4 ?
  1159.               (*tdev->procs->copy_mono)
  1160.                 (tdev, source, data_x, raster, gx_no_bitmap_id,
  1161.                  state.rect.x, state.rect.y - y0,
  1162.                  state.rect.width, state.rect.height,
  1163.                  state.color0, state.color1) :
  1164.               (*tdev->procs->copy_color)
  1165.                 (tdev, source, data_x, raster, gx_no_bitmap_id,
  1166.                  state.rect.x, state.rect.y - y0,
  1167.                  state.rect.width, state.rect.height));
  1168.             break;
  1169.            }
  1170.         if ( code < 0 ) return_error(code);
  1171.        }
  1172. }
  1173. /* The typical implementations of fread and fseek */
  1174. /* are extremely inefficient for small counts, */
  1175. /* so we use loops instead. */
  1176. private void
  1177. clist_read(FILE *f, byte *str, uint len)
  1178. {    switch ( len )
  1179.        {
  1180.     default: fread(str, 1, len, f); break;
  1181.     case 8: *str++ = (byte)getc(f);
  1182.     case 7: *str++ = (byte)getc(f);
  1183.     case 6: *str++ = (byte)getc(f);
  1184.     case 5: *str++ = (byte)getc(f);
  1185.     case 4: *str++ = (byte)getc(f);
  1186.     case 3: *str++ = (byte)getc(f);
  1187.     case 2: *str++ = (byte)getc(f);
  1188.     case 1: *str = (byte)getc(f);
  1189.        }
  1190. }
  1191. /* Read a short bitmap */
  1192. private cb_ptr
  1193. clist_read_short_bits(FILE *file, byte *data, register int bwidth, int height,
  1194.   cb_ptr cbp, cb_ptr cb_end)
  1195. {    uint bytes = bwidth * height;
  1196.     byte *pdata = data + bytes;
  1197.     byte *udata = data + (height << 2);
  1198.     cmd_read(data, bytes, cbp);
  1199.     while ( --height > 0 )        /* first row is in place already */
  1200.        {    udata -= 4, pdata -= bwidth;
  1201.         switch ( bwidth )
  1202.            {
  1203.         case 3: udata[2] = pdata[2];
  1204.         case 2: udata[1] = pdata[1];
  1205.         case 1: udata[0] = pdata[0];
  1206.            }
  1207.        }
  1208.     return cbp;
  1209. }
  1210.